﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.WizardFramework;
using Microsoft.VisualStudio.SharePoint.ProjectExtensions.Wizards;
using System.Diagnostics;
using System.Windows.Forms;

namespace SPVSX.Project.Extensions.Wizards
{
    class BaseWizardPage : WizardPage
    {
        // Fields
        private bool _isLoadingView;

        // Methods
        public BaseWizardPage()
        {
        }

        protected BaseWizardPage(ArtifactWizardForm wizard)
            : base(wizard)
        {
            base.ShowInfoPanel = false;
        }

        protected virtual void ApplyChangesToPresentationModel()
        {
        }

        protected override void Dispose(bool disposing)
        {
            try
            {
            }
            finally
            {
                base.Dispose(disposing);
            }
        }

        protected virtual bool IsCompletelyValid()
        {
            return true;
        }

        protected void LoadSettings()
        {
            if (!IsLoadingView)
            {
                try
                {
                    IsLoadingView = true;
                    LoadSettingsFromPresentationModel();
                }
                finally
                {
                    IsLoadingView = false;
                }
            }
        }

        protected virtual void LoadSettingsFromPresentationModel()
        {
        }

        public override void OnActivated()
        {
            try
            {
                Cursor.Current = Cursors.WaitCursor;
                base.OnActivated();
                LoadSettings();
                //base.Focus();
                if ((Wizard.NextPage != null) && !Wizard.NextPage.Visited)
                {
                    Wizard.DefaultButton = ButtonType.Next;
                }
            }
            catch (Exception exception)
            {
                //WizardHelpers.Logger.LogException(exception);
                //RtlAwareMessageBox.ShowError(null, exception.Message, WizardResources.TemplateWizardErrorCaption);
                throw;
            }
            finally
            {
                Cursor.Current = Cursors.Default;
                //CodeMarkers.Instance.CodeMarker(CodeMarkerEvent.perfSharePointWizardPageFlipEnd);
                //CodeMarkers.Instance.CodeMarker(CodeMarkerEvent.perfSharePointWizardLaunchEnd);
            }
        }

        public override bool OnDeactivate()
        {
            bool flag;
            try
            {
                //CodeMarkers.Instance.CodeMarker(CodeMarkerEvent.perfSharePointWizardPageFlipStart);
                Cursor.Current = Cursors.WaitCursor;
                flag = (base.OnDeactivate() && IsDataValid) && IsCompletelyValid();
            }
            catch (Exception exception)
            {
                //WizardHelpers.Logger.LogException(exception);
                //RtlAwareMessageBox.ShowError(null, exception.Message, WizardResources.TemplateWizardErrorCaption);
                throw;
            }
            finally
            {
                Cursor.Current = Cursors.Default;
            }
            return flag;
        }

        public override void OnDeactivated()
        {
            try
            {
                ApplyChangesToPresentationModel();
                base.OnDeactivated();
            }
            catch (Exception exception)
            {
                //WizardHelpers.Logger.LogException(exception);
                //RtlAwareMessageBox.ShowError(null, exception.Message, WizardResources.TemplateWizardErrorCaption);
                throw;
            }
        }

        // Properties
        protected bool IsLoadingView
        {
            get
            {
                return _isLoadingView;
            }
            set
            {
                _isLoadingView = value;
            }
        }

        public ArtifactWizardForm Wizard
        {
            [DebuggerStepThrough]
            get
            {
                return (ArtifactWizardForm)base.Wizard;
            }
        }
    }
}
